//1.引入样式表
//公用样式 
require('../../assets/css/index.less');
require('../../assets/fonts/iconfont.css');
// weui
require("../../lib/weui/weui.css")
let weui = require("../../lib/weui/weui.js")
// 当前页面样式
require('./personal.less')

//2.引入工具函数 utils
const dom = require('../../utils/dom');
const https = require('../../utils/https');
const local = require('../../utils/local');
// 轮播图
//ES6方式
import Swiper, { Pagination, Autoplay } from 'swiper';
//引入swiper样式
import 'swiper/swiper-bundle.min.css';
Swiper.use([Pagination, Autoplay])
// 3.当前页面的js
dom.ready(function () {
    dom.addEvent("#backOut",function(){
        history.go(-1)
    })
    dom.addEvent("#imgBtn", function () {
        dom.getEle("#fileBtn").click()
    })
    let imgUrl = '';
    dom.addEvent("#fileBtn", function () {
        // console.dir(this);
        // let file = this.files[0]
        // console.log(file);
        // let fd = new FormData()
        // fd.append("file", file)
        // console.log(fd);
        // https.post("/api/circles/uploadImg", fd, function (res) {
        // let { errno, data } = res
        // console.log(res);
        // if (errno == 0) {
        // console.log(data.url);
        // dom.getEle("#imgBtn").src = ' http://fitness.h5.itsource.cn:3701' + data.url
        // }
        // })
        dom.file("/api/circles/uploadImg", '#fileBtn', function (res) {
            let { errno, data } = res
            if (errno == 0) {
                dom.getEle("#imgBtn").src = 'http://fitness.h5.itsource.cn:3701' + data.url
                imgUrl = data.url
            }
        })
    }, "change")
    
    dom.addEvent("#sexBtn", function () {
        weui.picker([{
            label: '女',
            value: 0
        }, {
            label: '男',
            value: 1
        }], {
            // onChange: function (result) {
            //     console.log(result);
            // },
            onConfirm: function (result) {
                console.log(result);
                dom.getEle("#sexText").textContent = result[0].label
            },//点击确认
            title: '性别',
            showClose: false
        });
    })
    dom.addEvent("#btdBtn", function () {
        weui.datePicker({
            start: 1949,
            end: new Date().getFullYear(),
            // onChange: function (result) {
            //     console.log(result); 
            // },
            onConfirm: function (result) {
                console.log(result);
                dom.getEle("#btdText").textContent = result.map(v => dom.zeroize(v)).join("-")
            },
            title: '生日'
        });
    })
    let proValue = ''
    dom.addEvent("#proBtn", function () {
        https.get("/api/shared/province", function (res) {
            let { errno, data } = res
            console.log(res);
            if (errno == 0) {
                weui.picker(
                    data.map((v) => {
                        return {
                            label: v.name,
                            value: v.id
                        }
                    }), {
                    onConfirm: function (result) {
                        console.log(result);
                        dom.getEle("#proText").textContent = result[0].label
                        proValue = result[0].value
                        cityValue=''
                        dom.getEle("#cityText").textContent=''
                    },//点击确认
                    title: '省份',
                });
            }
        })
    })
    let cityValue = ''
    dom.addEvent("#cityBtn", function () {
        if (proValue) {
            https.get(`/api/shared/city?provinceId=${proValue}`, function (res) {
                let { errno, data } = res
                console.log(res);
                if (errno == 0) {
                    weui.picker(
                        data.map((v) => {
                            return {
                                label: v.name,
                                value: v.id
                            }
                        }), {
                        onConfirm: function (result) {
                            console.log(result);
                            dom.getEle("#cityText").textContent = result[0].label
                            cityValue = result[0].value
                        },//点击确认
                        title: '城市',
                    });
                }
            })
        }
    })
    dom.addEvent("#intro", function () {
        dom.getEle("#intro").value = dom.getEle("#intro").value.slice(0, 200);
        dom.getEle("#introNum").textContent = dom.getEle("#intro").value.length;
    }, "input")
    dom.addEvent("#saveBtn", function () {
        let obj = {
            nickName: dom.getEle("#nickname").value,
            gender: sexText.textContent == "男" ? 1 : 0,
            provinceId: proValue,
            cityId: cityValue,
            birthday: btdText.textContent,
            imgUrl: imgUrl,
            sign: dom.getEle("#intro").value,
        };
        console.log(obj);
        https.post("/api/user/changeInfo", obj, function (res) {
            console.log(res);
            if (res.errno == 0) {
                //提示
                // weui.toast('保存成功', 2000);
                dom.getEle("#toast").style.display = "block"
                setTimeout(function () {
                    dom.getEle("#toast").style.display = "none"
                }, 1000);
                //更新token
                local.set('tk', res.data.token)
                //跳转到 我的页面
                location.href = 'mine.html'
            } else {
                // weui.fadeIn('保存失败', 2000);
                dom.getEle("#warnToast").style.display = "block"
                setTimeout(function () {
                    dom.getEle("#warnToast").style.display = "none"
                }, 1000);
            }
        })
    })
    function render() {
        let userData = local.get("userData")
        console.log(userData);
        dom.getEle("#imgBtn").src = 'http://fitness.h5.itsource.cn:3701' + userData.imgUrl
        dom.getEle("#nickname").value = userData.nickName;
        dom.getEle("#intro").textContent = userData.sign;
        dom.getEle("#sexText").textContent = userData.gender == 1 ? "男" : "女"
        dom.getEle("#btdText").textContent = userData.birthday;
        imgUrl=userData.imgUrl
    }
    render()

    https.get("/api/shared/province", res => {
        // console.log(res);
        let province = res.data.filter(v => {
            return v.id == local.get("userData").provinceId
        })
        // console.log(province);
        dom.getEle("#proText").textContent = province[0].name
        proValue =province[0].id
        https.get(`/api/shared/city?provinceId=${province[0].id}`, res => {
            // console.log(res);
            let city = res.data.filter(v => {
                return v.id == local.get("userData").cityId
            })
            dom.getEle("#cityText").textContent = city[0].name
            cityValue =city[0].id
        })
    })
})
